import { forwardRef, useImperativeHandle, useState } from 'react';

import { useRequest } from 'ahooks';
import { Button, DatePicker, Drawer, Form, FormProps, InputNumber, Space } from 'antd';
import dayjs from 'dayjs';

import { store } from '@/store';

import {
  proDataAgentPriceAdd,
  proDataAgentPriceUpdate
} from '@/services/bizApi/yaojixiandanjiajiekou';

import { BizObject } from '.';

type EditOpenBaseArgs = {
  /** 成功回调 */
  onSuccess?: () => void;
};

export type EditRef = {
  open: (
    arg:
      | (EditOpenBaseArgs & {
          type: 'add';
        })
      | (EditOpenBaseArgs & {
          type: 'edit';
          rowData: BizObject;
        })
      | (EditOpenBaseArgs & {
          type: 'detail';
          rowData: BizObject;
        })
  ) => void;
};

export type EditProps = {};

const Edit: React.ForwardRefRenderFunction<EditRef, EditProps> = (_, ref) => {
  const [form] = Form.useForm();
  // 弹窗开启状态
  const [open, setOpen] = useState(false);
  const [args, setArgs] = useState<ArgumentsType<EditRef['open']>[0]>();
  const [weekRange, setWeekRange] = useState({
    startDate: '',
    endDate: ''
  });

  const submit = useRequest(
    async ({ _type, ...values }) => {
      const query = {
        ...values,
        startDate: weekRange?.startDate,
        endDate: weekRange?.endDate
      };
      if (args?.type === 'edit') {
        // 编辑接口
        await proDataAgentPriceUpdate({
          ...query,

          id: args?.rowData?.id
        });
      } else {
        await proDataAgentPriceAdd(query);
      }

      args?.onSuccess?.();
      setOpen(false);

      store.app?.message.success(args?.type === 'add' ? '新增成功' : '编辑成功');
    },
    {
      manual: true
    }
  );

  useImperativeHandle(ref, () => {
    return {
      open: (arg) => {
        setArgs(arg);
        setOpen(true);

        setTimeout(() => {
          // 编辑回填数据
          if ((arg?.type === 'edit' || arg?.type === 'detail') && arg?.rowData) {
            setWeekRange({
              startDate: arg.rowData?.startDate as string,
              endDate: arg.rowData?.endDate as string
            });
            // 回填数据
            form.setFieldsValue({
              ...arg.rowData,
              rq: [dayjs(arg.rowData?.startDate), dayjs(arg.rowData?.endDate)]
              // rq: dayjs(arg.rowData?.rq)
            });
          } else {
            form.resetFields();
          }
        });
      }
    };
  });

  const onFinish: FormProps['onFinish'] = async (values) => {
    return submit.runAsync(values);
  };

  const onCancel = () => {
    if (submit.loading) {
      store.app?.message.info('正在提交中，请稍后');
      return;
    }
    setOpen(false);
  };

  // 仅保存
  const onSave = () => {
    form.submit();
  };

  const handleWeekSelect = (date: any) => {
    if (!date) {
      setWeekRange({ startDate: '', endDate: '' });
      return;
    }

    // 计算周开始日期（周一）
    const startDate = date.startOf('week');
    // 计算周结束日期（周日）
    const endDate = date.endOf('week');
    // 获取周数
    console.log(date.week());
    setWeekRange({
      startDate: startDate.format('YYYY-MM-DD'),
      endDate: endDate.format('YYYY-MM-DD')
    });
  };

  return (
    <Drawer
      title={
        {
          add: '新增',
          edit: '编辑',
          detail: '详情'
        }[args?.type as string]
      }
      extra={
        <>
          {args?.type !== 'detail' && (
            <Space>
              <Button
                type="primary"
                loading={submit.loading}
                disabled={submit.loading}
                onClick={onSave}
              >
                确定
              </Button>
              <Button onClick={onCancel} disabled={submit.loading}>
                取消
              </Button>
            </Space>
          )}
        </>
      }
      width="50%"
      open={open}
      onClose={onCancel}
    >
      <Form
        onFinish={onFinish}
        form={form}
        layout="horizontal"
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 14 }}
        disabled={args?.type === 'detail'}
        requiredMark={args?.type !== 'detail'}
        variant={args?.type === 'detail' ? 'borderless' : undefined}
      >
        <Form.Item
          label="日期"
          name="rq"
          rules={[
            {
              required: true,
              message: '请选择周'
            }
          ]}
        >
          <DatePicker
            style={{ width: '100%' }}
            picker="week"
            allowClear={false}
            format="YYYY-w周"
            onCalendarChange={handleWeekSelect}
            placeholder={'请选择周'}
          />
        </Form.Item>

        {/* <Form.Item
          label="药剂用量(合计)"
          name="yjyl"
          rules={[
            {
              required: true,
              message: '请输入药剂用量(合计)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入药剂用量(合计)"
          />
        </Form.Item> */}

        <Form.Item
          label="植物除臭药剂"
          name="zwccyj"
          rules={[
            {
              required: true,
              message: '请输入植物除臭药剂'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入植物除臭药剂"
          />
        </Form.Item>

        <Form.Item
          label="次氯酸钠"
          name="clsd"
          rules={[
            {
              required: true,
              message: '请输入次氯酸钠'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入次氯酸钠"
          />
        </Form.Item>

        <Form.Item
          label="等效碳源"
          name="dxtr"
          rules={[
            {
              required: true,
              message: '请输入等效碳源'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入等效碳源"
          />
        </Form.Item>

        <Form.Item
          label="复合型碳源"
          name="fhxtr"
          rules={[
            {
              required: true,
              message: '请输入复合型碳源'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入复合型碳源"
          />
        </Form.Item>

        <Form.Item
          label="葡萄糖"
          name="ptj"
          rules={[
            {
              required: true,
              message: '请输入葡萄糖'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入葡萄糖" />
        </Form.Item>

        <Form.Item
          label="聚氯化铝"
          name="jlhhl"
          rules={[
            {
              required: true,
              message: '请输入聚氯化铝'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入聚氯化铝"
          />
        </Form.Item>

        <Form.Item
          label="七水硫酸亚铁"
          name="qslsytt"
          rules={[
            {
              required: true,
              message: '请输入七水硫酸亚铁'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入七水硫酸亚铁"
          />
        </Form.Item>

        <Form.Item
          label="除磷剂"
          name="cpj"
          rules={[
            {
              required: true,
              message: '请输入除磷剂'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入除磷剂" />
        </Form.Item>

        <Form.Item
          label="碱"
          name="j"
          rules={[
            {
              required: true,
              message: '请输入碱'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入碱" />
        </Form.Item>

        <Form.Item
          label="片碱"
          name="pj"
          rules={[
            {
              required: true,
              message: '请输入片碱'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入片碱" />
        </Form.Item>

        <Form.Item
          label="液碱"
          name="yj"
          rules={[
            {
              required: true,
              message: '请输入液碱'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入液碱" />
        </Form.Item>

        <Form.Item
          label="稀硫酸"
          name="xls"
          rules={[
            {
              required: true,
              message: '请输入稀硫酸'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入稀硫酸" />
        </Form.Item>

        <Form.Item
          label="盐酸"
          name="ys"
          rules={[
            {
              required: true,
              message: '请输入盐酸'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入盐酸" />
        </Form.Item>

        <Form.Item
          label="阳离子聚丙烯酰胺(气浮)"
          name="ylzblxyamqf"
          rules={[
            {
              required: true,
              message: '请输入阳离子聚丙烯酰胺(气浮)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入阳离子聚丙烯酰胺(气浮)"
          />
        </Form.Item>

        <Form.Item
          label="阳离子聚丙烯酰胺(脱泥)"
          name="ylzblxyamdn"
          rules={[
            {
              required: true,
              message: '请输入阳离子聚丙烯酰胺(脱泥)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入阳离子聚丙烯酰胺(脱泥)"
          />
        </Form.Item>

        <Form.Item
          label="双氧水"
          name="dyy"
          rules={[
            {
              required: true,
              message: '请输入双氧水'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入双氧水" />
        </Form.Item>

        <Form.Item
          label="消泡剂"
          name="xpj"
          rules={[
            {
              required: true,
              message: '请输入消泡剂'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入消泡剂" />
        </Form.Item>

        <Form.Item
          label="柠檬酸"
          name="mlss"
          rules={[
            {
              required: true,
              message: '请输入柠檬酸'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入柠檬酸" />
        </Form.Item>

        <Form.Item
          label="膜清洗剂"
          name="mqxj"
          rules={[
            {
              required: true,
              message: '请输入膜清洗剂'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入膜清洗剂"
          />
        </Form.Item>

        <Form.Item
          label="纳滤阻垢剂"
          name="nlfzgj"
          rules={[
            {
              required: true,
              message: '请输入纳滤阻垢剂'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入纳滤阻垢剂"
          />
        </Form.Item>

        <Form.Item
          label="膜清洗剂(酸性)"
          name="mqxjsx"
          rules={[
            {
              required: true,
              message: '请输入膜清洗剂(酸性)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入膜清洗剂(酸性)"
          />
        </Form.Item>

        <Form.Item
          label="膜清洗剂(碱性)"
          name="mqxjjx"
          rules={[
            {
              required: true,
              message: '请输入膜清洗剂(碱性)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入膜清洗剂(碱性)"
          />
        </Form.Item>

        <Form.Item
          label="洛碛渗滤液非氧化杀菌剂"
          name="lqslfyyxj"
          rules={[
            {
              required: true,
              message: '请输入洛碛渗滤液非氧化杀菌剂'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入洛碛渗滤液非氧化杀菌剂"
          />
        </Form.Item>

        <Form.Item
          label="氯化铁液体"
          name="lhtyt"
          rules={[
            {
              required: true,
              message: '请输入氯化铁液体'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入氯化铁液体"
          />
        </Form.Item>

        <Form.Item
          label="水费(吨)"
          name="sf"
          rules={[
            {
              required: true,
              message: '请输入水费(吨)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入水费(吨)"
          />
        </Form.Item>

        <Form.Item
          label="餐厨污水厂(水费)"
          name="sfCcwsc"
          rules={[
            {
              required: true,
              message: '请输入餐厨污水厂(水费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入餐厨污水厂(水费)"
          />
        </Form.Item>

        <Form.Item
          label="洛碛渗滤液污水厂(水费)"
          name="sfLqslfywsc"
          rules={[
            {
              required: true,
              message: '请输入洛碛渗滤液污水厂(水费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入洛碛渗滤液污水厂(水费)"
          />
        </Form.Item>

        <Form.Item
          label="黑石子污水厂(水费)"
          name="sfHszwsc"
          rules={[
            {
              required: true,
              message: '请输入黑石子污水厂(水费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入黑石子污水厂(水费)"
          />
        </Form.Item>

        <Form.Item
          label="界石污水厂(水费)"
          name="sfJswsc"
          rules={[
            {
              required: true,
              message: '请输入界石污水厂(水费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入界石污水厂(水费)"
          />
        </Form.Item>

        <Form.Item
          label="走马污水厂(水费)"
          name="sfZmwsc"
          rules={[
            {
              required: true,
              message: '请输入走马污水厂(水费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入走马污水厂(水费)"
          />
        </Form.Item>

        <Form.Item
          label="夏家坝污水厂(水费)"
          name="sfXjbwsc"
          rules={[
            {
              required: true,
              message: '请输入夏家坝污水厂(水费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入夏家坝污水厂(水费)"
          />
        </Form.Item>

        <Form.Item
          label="电费(度)"
          name="df"
          rules={[
            {
              required: true,
              message: '请输入电费(度)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入电费(度)"
          />
        </Form.Item>

        <Form.Item
          label="餐厨污水厂(电费)"
          name="dfCcwsc"
          rules={[
            {
              required: true,
              message: '请输入餐厨污水厂(电费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入餐厨污水厂(电费)"
          />
        </Form.Item>

        <Form.Item
          label="洛碛渗滤液污水厂(电费)"
          name="dfLqslfywsc"
          rules={[
            {
              required: true,
              message: '请输入洛碛渗滤液污水厂(电费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入洛碛渗滤液污水厂(电费)"
          />
        </Form.Item>

        <Form.Item
          label="黑石子污水厂(电费)"
          name="dfHszwsc"
          rules={[
            {
              required: true,
              message: '请输入黑石子污水厂(电费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入黑石子污水厂(电费)"
          />
        </Form.Item>

        <Form.Item
          label="界石污水厂(电费)"
          name="dfJswsc"
          rules={[
            {
              required: true,
              message: '请输入界石污水厂(电费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入界石污水厂(电费)"
          />
        </Form.Item>

        <Form.Item
          label="走马污水厂(电费)"
          name="dfZmwsc"
          rules={[
            {
              required: true,
              message: '请输入走马污水厂(电费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入走马污水厂(电费)"
          />
        </Form.Item>

        <Form.Item
          label="夏家坝污水厂(电费)"
          name="dfXjbwsc"
          rules={[
            {
              required: true,
              message: '请输入夏家坝污水厂(电费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入夏家坝污水厂(电费)"
          />
        </Form.Item>

        <Form.Item
          label="原水厂(电费)"
          name="dfYsc"
          rules={[
            {
              required: true,
              message: '请输入原水厂(电费)'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入原水厂(电费)"
          />
        </Form.Item>

        <Form.Item
          label="污泥外运"
          name="wnwy"
          rules={[
            {
              required: true,
              message: '请输入污泥外运'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入污泥外运"
          />
        </Form.Item>

        <Form.Item
          label="污泥填埋"
          name="wntm"
          rules={[
            {
              required: true,
              message: '请输入污泥填埋'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入污泥填埋"
          />
        </Form.Item>

        <Form.Item
          label="污泥热水解"
          name="wnrsj"
          rules={[
            {
              required: true,
              message: '请输入污泥热水解'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入污泥热水解"
          />
        </Form.Item>

        <Form.Item
          label="取水费"
          name="qsf"
          rules={[
            {
              required: true,
              message: '请输入取水费'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入取水费" />
        </Form.Item>
      </Form>
    </Drawer>
  );
};

export default forwardRef(Edit);
